<template>
	<div class="boxStyle">
		<el-row type="flex" class="row-bg" justify="start" style="margin-bottom: 10px;">
			<el-col :span="1" v-if="addShow!==-1">
				<el-button type="warning" size="small" @click="add" icon="el-icon-plus">新增</el-button>
			</el-col>
		</el-row>
		<el-table ref="multipleTable" border stripe :data="tableLists" tooltip-effect="dark" style="margin-bottom: 10px;width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="adminName" label="用户名" width="120"></el-table-column>
			<el-table-column prop="sysOrganization.name" label="所属机构" width="120"></el-table-column>
			<el-table-column prop="sysRole.description" label="角色" width="120"></el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" @click="update(scope.$index,scope.row.id)" v-if="updateShow!==-1">更新信息</el-button>
					<el-button size="mini" type="success" @click="changePass(scope.row.id)" v-if="chgpShow!==-1">修改密码</el-button>
					<el-button size="mini" type="danger" @click="del(scope.row.id)" v-if="scope.row.id!==1&&deleShow!==-1">删除信息</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination background @current-change="handleCurrentChange" :current-page="currentPage" :page-size="size" layout="prev, pager, next,total, slot" :total="totals">
			<span style="margin-top: -2px;color: #606266;font-weight: 100;">第<el-input type="text" v-model="inputPage" style="width: 60px;margin: 0 5px;"/>页<el-button size="mini" style="background: #409EFF; color: #fff;margin:0 5px;" @click="handleCurrentChange(inputPage)">跳转</el-button><span>总计{{totalPage}}页</span></span>
		</el-pagination>
		<!--新增和更新弹窗-->
		<div class="modal fade" id="adm" tabindex="-1" role="dialog" data-keyboard="false">
			<div class="modal-dialog" role="document">
				<div class="modal-content" style="width:800px;height: 300px;">
					<div class="modal-header" style="border-radius: 4px;">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
						<ul class="nav nav-tabs adm" role="tablist">
							<li role="presentation" class="active">
								<a href="#qusInfo" aria-controls="qusInfo" role="tab" data-toggle="tab">新增/编辑</a>
							</li>
						</ul>
					</div>
					<div class="modal-body" style="padding-right: 60px;height:170px;overflow: hidden;overflow-y: auto;">
						<div class="tab-content">
							<div role="tabpanel" class="tab-pane active" id="qusInfo">
								<el-form :model="ruleAdm" :rules="rules" ref="ruleAdm" label-width="100px" class="demo-ruleForm">
									<input type="hidden" name="id" v-model="id">
									<el-col :span="12">
										<el-form-item label="用户名:" prop="adminName">
											<el-input v-model="ruleAdm.adminName" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12" v-if="passShow">
										<el-form-item label="密码:" prop="admin_pass">
											<el-input v-model="ruleAdm.admin_pass" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="机构:" prop="organizationId">
											<el-select v-model="ruleAdm.organizationId" size="small" style="width: 260px;">
												<el-option v-for="item in org" :key="item.id" :label="item.name" :value="item.id">
												</el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="角色:" prop="roleId">
											<el-select v-model="ruleAdm.roleId" size="small" style="width: 260px;">
												<el-option v-for="item in role" :key="item.id" :label="item.description" :value="item.id">
												</el-option>
											</el-select>
										</el-form-item>
									</el-col>
								</el-form>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-success" id="save" @click="save('ruleAdm')">保存</button>
						<button type="button" class="btn btn-success" id="usave" @click="uSave('ruleAdm')">保存</button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
				</div>
			</div>
			<div></div>
		</div>
		<!--修改密码-->
		<div class="modal fade" id="admPass" tabindex="-1" role="dialog" data-keyboard="false">
			<div class="modal-dialog" role="document">
				<div class="modal-content" style="width:800px;height: 230px;">
					<div class="modal-header" style="border-radius: 4px;">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
						<ul class="nav nav-tabs adm" role="tablist">
							<li role="presentation" class="active">
								<a href="#qusInfo" aria-controls="qusInfo" role="tab" data-toggle="tab">修改密码</a>
							</li>
						</ul>
					</div>
					<div class="modal-body" style="padding-right: 60px;height:100px;overflow: hidden;overflow-y: auto;">
						<div class="tab-content">
							<div role="tabpanel" class="tab-pane active" id="qusInfo">
								<el-form :model="rulepass" :rules="rules" ref="rulepass" label-width="100px" class="demo-ruleForm">
									<input type="hidden" v-model="passid">
									<el-form-item label="新密码:" prop="newPass">
										<el-input v-model="rulepass.newPass" size="small" type="password"></el-input>
									</el-form-item>
								</el-form>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-success" id="save" @click="passSave('rulepass')">保存</button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
				</div>
			</div>
			<div></div>
		</div>
	</div>
</template>

<script>
	import axios from "axios"
	import qs from "qs"
	import { NAMENOT_NULL } from "static/js/rules.js"
	export default {
		data() {
			return {
				tableLists: [],
				purl: this.$store.state.purl,
				currentPage: 1,
				totals: 0, //分页总条数
				size: 0, //每页条数
				updateShow: 0, //是否显示更新信息
				addShow: 0, //是否显示新增
				deleShow:0,
				passShow: 1,
				chgpShow:0,
				id: "",
				passid: "",
				org: [],
				role: [],
				ruleAdm: {
					adminName: "",
					admin_pass: "",
					organizationId: 1,
					roleId: 1
				},
				rules: {
					name: NAMENOT_NULL,
					newPass: {
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					},
					admin_pass: {
						required: true,
						message: '请输入密码',
						trigger: 'change'
					},
					adminName: {
						required: true,
						message: '请输入用户名',
						trigger: 'change'
					},
				},
				rulepass: {
					newPass: ""
				},
				inputPage: 1,
				totalPage: 0, //总页数
			}
		},
		activated() {
			this.load(1);
			this.currentPage =1;
			this.inputPage =this.currentPage;
			this.addShow = this.purl[0].findIndex((val) => val === "/interface/admin/showCreateForm") //判断是否拥有增加权限
			this.updateShow = this.purl[0].findIndex((val) => val === "/interface/admin/showUpdateForm") //判断是否拥有更新权限
			this.deleShow = this.purl[0].findIndex((val) => val === "/interface/admin/delete")
			this.chgpShow = this.purl[0].findIndex((val) => val === "/interface/admin/updatePassWordForm")
		},
		methods: {
			load(num) {
				axios.get("interface/admin?pageNo=" + num + "").then((res) => {
					this.tableLists = res.data.data.sysAdmins; //list数据
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})
			},
			handleCurrentChange(val) {
				this.inputPage = val;
				this.currentPage = val-0;
				axios.get("interface/admin?pageNo=" + val + "").then((res) => {
					this.tableLists = res.data.data.sysAdmins;
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})
			},
			add() {
				$("#adm").modal("show");
//				$(".adm li a").text("新增角色信息")
				$("#save").show();
				$("#usave").hide();
				this.passShow = 1;
				this.ruleAdm.adminName = "";
				this.ruleAdm.organizationId=1;
				this.ruleAdm.roleId=1;
				this.ruleAdm.admin_pass = "";
				axios.get("interface/admin/showCreateForm").then((res) => {
					this.org = res.data.data.sysOrganizations;
					this.role = res.data.data.sysRoles;
				})
			},
			save(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						var params = {
							adminName: this.ruleAdm.adminName,
							password: this.ruleAdm.admin_pass,
							organizationId: this.ruleAdm.organizationId,
							roleId: this.ruleAdm.roleId
						}
						axios({
							method: 'post',
							url: 'interface/admin/save',
							data: params,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.load(this.currentPage);
								$("#adm").modal("hide");
								this.$message.success("新增成功");
							} else {
								$("#adm").modal("hide");
								this.$alert(res.data.msg, '提示', {
									confirmButtonText: '确定',
									type: 'warning',
									callback: action => {
										$("#adm").modal("show");
									}
								})
							}
						});
					} else {
						return false;
					}
				})
			},
			update(index, id) {
				$("#adm").modal("show");
//				$(".adm li a").text("更新角色信息")
				$("#save").hide();
				$("#usave").show();
				this.passShow = 0;
				this.id = id;
				this.ruleAdm.adminName = this.tableLists[index].adminName;
				this.ruleAdm.admin_pass = this.tableLists[index].password;
				this.ruleAdm.organizationId = this.tableLists[index].organizationId;
				this.ruleAdm.roleId = this.tableLists[index].roleId
				axios.get("interface/admin/showUpdateForm?id=" + this.id).then((res) => {
					this.org = res.data.data.sysOrganizations;
					this.role = res.data.data.sysRoles;
				})
			},
			uSave(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						var params = {
							id:this.id,
							adminName: this.ruleAdm.adminName,
							password: this.ruleAdm.admin_pass,
							organizationId: this.ruleAdm.organizationId,
							roleId: this.ruleAdm.roleId
						}
						axios({
							method: 'post',
							url: 'interface/admin/save',
							data: params,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							this.load(this.currentPage);
							$("#adm").modal("hide");
							this.$message.success("更新成功");
						});
					} else {
						return false;
					}
				})
			},
			changePass(id) {
				$("#admPass").modal("show")
				this.rulepass.newPass = "";
				this.passid = id;
			},
			passSave(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						axios.post("interface/admin/savePassWord?", qs.stringify({
							id: this.passid,
							newPassWord: this.rulepass.newPass
						})).then((res) => {
							this.load(this.currentPage);
							$("#admPass").modal("hide")
							this.$message.success("修改成功");
						})
					} else {
						return false;
					}
				})
			},
			del(id) {
				this.$confirm('确认要删除此条数据吗?', '删除警告', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					axios.post("interface/admin/delete?id=" + id + "").then((res) => {
						this.load(1)
					})
					this.$message({
						type: 'success',
						message: '删除成功!'
					});
				});
			}
		}
	}
</script>

<style>

</style>